import {
    queryProducts
} from "../../services/order/orderProduct";
import Constants from '../../utils/Constants';
import { message } from "antd";
const R = require('ramda');
export default {
    namespace: 'orderProduct',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        allProducts: []
    },
    reducers: {
        /**
         * 设置产品数据列表
         *
         * @param state
         * @param action
         */
        setProductList(state, { payload }) {
            return { ...state, allProducts: payload.productList, page: payload.page }
        },
    },
    effects: {
        /**
         * 分页查询产品
         *
         * @param call
         * @param put
         * @param select
         */
        *queryProductList({ payload }, { call, put, select }) {
            let page = yield select(state => state.orderProduct.page);
            payload.page = R.isNil(payload.page) ? page : payload.page;
            payload.status = "4";
            const res = yield call(queryProducts, { ...R.omit(['OnSuccess', 'OnFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                let productList = R.isNil(content.list) ? [] : content.list;
                yield put({
                    type: 'setProductList',
                    payload: {
                        productList: productList,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

    }
};